package com.example.glide_pre_demo;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RemoteViews;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.ViewTarget;
import com.example.glide_pre_demo.manager.ImageLoader;
import com.example.glide_pre_demo.widget.FutureStudioView;

public class MainActivity extends AppCompatActivity {

    public static final String IMG_URL = "http://img3.cache.netease.com/photo/0005/2016-05-15/BN2S0SBP00DE0005.jpg";
//    public static final String IMG1 = "http://images.cnblogs.com/cnblogs_com/zhzhy126/729387/o_1.png";
    public static final String IMG1 = "http://img4.3lian.com/sucai2/img14/56/19.gif";
    public static final String SMALL_IMG_URL = "http://tse1.mm.bing.net/th?id=OIP.M88f56997adb943c55154b9182f78cbd6o0&pid=15.1";
    private static final int NOTIFICATION_ID = 1;


    private ImageView mIv1;
    private ImageView mIv2;
    private ImageView mIv3;
    private ImageView mIv4;
    private ImageView mIv5;

    private SimpleTarget mTarget = new SimpleTarget<Bitmap>(200, 200) {
        @Override
        public void onStart() {
            super.onStart();
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
        }

        @Override
        public void onLoadStarted(Drawable placeholder) {
            super.onLoadStarted(placeholder);
        }

        @Override
        public void onLoadFailed(Exception e, Drawable errorDrawable) {
            super.onLoadFailed(e, errorDrawable);
        }

        @Override
        public void onLoadCleared(Drawable placeholder) {
            super.onLoadCleared(placeholder);
        }

        @Override
        public void onStop() {
            super.onStop();
        }

        @Override
        public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {
            // do something with the bitmap
            // for demonstration purposes, let's just set it to an ImageView
            mIv4.setImageBitmap(bitmap);
        }
    };

    private SimpleTarget mTarget1 = new SimpleTarget<Bitmap>() {
        @Override
        public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {
            // do something with the bitmap
            // for demonstration purposes, let's just set it to an ImageView
            mIv5.setImageBitmap(bitmap);
        }
    };

    private ViewTarget mViewTarget;
    private FutureStudioView mFutureStudioView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initData();
        loadImage();

        loadNotification();
    }

    private void loadNotification() {
        final RemoteViews rv = new RemoteViews(MyApplication.getAppContext().getPackageName(),
                R.layout.layout_notification);
        rv.setImageViewResource(R.id.remoteview_notification_icon, R.drawable.ic_wx);
        rv.setTextViewText(R.id.remoteview_notification_headline, "Headline");
        rv.setTextViewText(R.id.remoteview_notification_short_message, "Short Message");

        // build notification
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(MyApplication.getAppContext())
                        .setSmallIcon(R.drawable.ic_wx)
                        .setContentTitle("Content Title")
                        .setContentText("Content Text")
                        .setContent(rv)
                        .setPriority( NotificationCompat.PRIORITY_MIN);

        final Notification notification = mBuilder.build();

        // set big content view for newer androids
        if (android.os.Build.VERSION.SDK_INT >= 16) {
            notification.bigContentView = rv;
        }

        NotificationManager mNotificationManager = (NotificationManager) MyApplication
                .getAppContext().getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(NOTIFICATION_ID, notification);
    }

    private void initView() {
        mIv1 = (ImageView) findViewById(R.id.iv1);
        mIv2 = (ImageView) findViewById(R.id.iv2);
        mIv3 = (ImageView) findViewById(R.id.iv3);
        mIv4 = (ImageView) findViewById(R.id.iv4);
        mIv5 = (ImageView) findViewById(R.id.iv5);
        mFutureStudioView = (FutureStudioView) findViewById(R.id.fsv);
    }

    private void initData() {
        mViewTarget = new ViewTarget<FutureStudioView, GlideDrawable>(mFutureStudioView) {
            @Override
            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                this.view.setImage(resource.getCurrent());
            }
        };
        mFutureStudioView.setText("标题");
    }

    private void loadImage() {
        ImageLoader.loadFromResource(MyApplication.getAppContext(), R.drawable.no1, mIv1);
        ImageLoader.loadFromUrl(MyApplication.getAppContext(), SMALL_IMG_URL, mIv2);
        ImageLoader.loadFromResourceUri(MyApplication.getAppContext(), R.drawable.child, mIv3);
        ImageLoader.loadFromUrl(MyApplication.getAppContext(), IMG_URL, mTarget);
        ImageLoader.loadFromUrl(MyApplication.getAppContext(), SMALL_IMG_URL, mTarget1);
        ImageLoader.loadImageViewTarget(mViewTarget, IMG1);
    }

    public void openImageListActivity(View view) {
        startActivity(new Intent(MainActivity.this, ImageActivity.class));
        overridePendingTransition(R.anim.enter, R.anim.exit);
    }

    public void showBlurImage(View view) {
        startActivity(new Intent(MainActivity.this, BlurActivity.class));
        overridePendingTransition(R.anim.enter, R.anim.exit);
    }

}